//
//  YYTopItemView.m
//  YYPopMenuView
//
//  Created by 蓓蕾 on 2021/8/23.
//

#import "YYTopItemView.h"
#import <Masonry.h>

@interface YYTopItemView ()

@property (nonatomic, strong) UILabel *titleView;
@property (nonatomic, strong) UIImageView *iconView;

@end

@implementation YYTopItemView

-(id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _allowSelected = YES;
//        UIView *itemView = [[UIView alloc] init];
//        [self addSubview:itemView];
//        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.right.mas_equalTo(self.mas_right).mas_offset(0);
//            make.top.mas_equalTo(self.mas_top).mas_offset(0);
//            make.bottom.mas_equalTo(self.mas_bottom).mas_offset(0);
//        }];
        
        _iconView = [[UIImageView alloc] init];
        [self addSubview:_iconView];
        _iconView.image = [UIImage imageNamed:@"arrow_down_grey"];
        [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.mas_right).mas_offset(-5);
            make.centerY.mas_equalTo(self.mas_centerY).mas_offset(0);
//            make.width.mas_equalTo(20);
//            make.height.mas_equalTo(20);
        }];
        
        _titleView = [[UILabel alloc] init];
        _titleView.textColor = [UIColor blackColor];
        _titleView.font = [UIFont systemFontOfSize:15];
        [self addSubview:_titleView];
        _titleView.text = @"貂蝉";
        [_titleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(_iconView.mas_left).mas_offset(-5);
//            make.left.mas_lessThanOrEqualTo(self.mas_left).mas_offset(5);
            make.centerY.mas_equalTo(self.mas_centerY).mas_offset(0);
        }];
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_titleView.mas_left).mas_offset(-5);
        }];
//        self.backgroundColor = [UIColor greenColor];
    }
    return self;
}

-(void)setItemMargin:(CGFloat)itemMargin
{
    _itemMargin = itemMargin;
    [_titleView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_iconView.mas_left).mas_offset(-itemMargin);
    }];
}

-(void)setImageSize:(CGSize)imageSize
{
    _imageSize = imageSize;
    [_iconView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(imageSize);
    }];
}

-(void)setTitleString:(NSString *)titleString
{
    _titleString = titleString;
    _titleView.text = titleString;
}

-(void)setNormalIconString:(NSString *)normalIconString
{
    _normalIconString = normalIconString;
    if (!_selected) {
        _iconView.image = [UIImage imageNamed:normalIconString];
    }
}

-(void)setSelectIconString:(NSString *)selectIconString
{
    _selectIconString = selectIconString;
    if (_selected) {
        _iconView.image = [UIImage imageNamed:selectIconString];
    }
}

-(void)setSelected:(BOOL)selected
{
    _selected = selected;
    if (_allowSelected) {
        if (selected) {
    //        _iconView.image = [UIImage imageNamed:_selectIconString];
            NSLog(@"旋转180");
            _iconView.transform = CGAffineTransformRotate(_iconView.transform, M_PI);//旋转180
        }
        else
        {
    //        _iconView.image = [UIImage imageNamed:_normalIconString];
            NSLog(@"还原");
            _iconView.transform = CGAffineTransformIdentity;//还原
        }
    }
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
